// 登录混合js
import {register} from "@/utils/plugins/resize";
import {getCsrf,getToken} from '@/api/system/login'
import {prefix,serviceName} from "@/const.config.js";
import {SET_TOKEN} from "@/store/types/user";
import {getRsaCode} from "@/utils/func/encrypt";

export default {
  data() {
    return {
      dialogChangePasdVisible: false,
      logging:false,
      form: {
        "grant_type": "password",
        "scope": "all",
        "username": "administrator",
        "password": "123456",
        "validcode":"",
        "csrf":""
        // "client_id":"test",
        // "client_secret":"aabbcc",
        // loginName: "administrator",
        // password: "123456",
        // name: "administrator",
        // secret: "123456",
        // grantTypes: "password",
        // scope: "view",
        // code: "",
        // isAutoLogin: false,
      },
      rules: {
        username: [
          {required: true, message: "请输入您的用户名", trigger: "blur"},
        ],
        password: [
          {required: true, message: "请输入您的密码", trigger: "blur"},
        ],
        validcode: [{required: true, message: "请输入验证码", trigger: "blur"}],
      },
      isMobile: false,
      clientId:"test",
      csrfCode:""
    };
  },
  mounted() {
    this.$ls.remove('token')
    this.$store.dispatch(SET_TOKEN,{})
    register(this._uid, this.resize)
    this.getCsrf()
  },
  computed: {
    csrfCodeUrl() {
      if(this.clientId && this.csrfCode){
        return prefix + serviceName+'/rest/system/common/createValidCode?clientName='+this.clientId+'&csrfCode='+this.csrfCode
      }else {
        return ""
      }
    },
  },
  methods: {
    /**
     * 首页登录接口
     * @param {Object} formName
     */
    handleSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.logging = true;
          const form = JSON.parse(JSON.stringify(this.form))
          form.password = getRsaCode(form.password)
          form.csrf = this.csrfCode
          getToken(form,{ Authorization:"Basic "+ getRsaCode("test:aabbcc")}).then((res) => {
            this.$ls.set('token', res.data,res.data.expires_in * 1000)
            this.$store.dispatch(SET_TOKEN,res.data)
            this.$router.push(this.$route.query.redirect || "/");
          }).catch(()=>{
            this.logging = false;
          })
        }
      });
    },
    /**
     * 验证码更换图片
     */
    updateCode() {
      this.getCsrf()
    },
    /**
     * 屏幕发生改变修改宽度
     * @param {String} type  栅格尺寸
     * @param {Number} width 屏幕宽度
     */
    resize(type, width) {
      if (width < 750) {
        if (!this.isMobile) {
          this.isMobile = true;
        }
      } else {
        if (this.isMobile) {
          this.isMobile = false;
        }
      }
    },
    /**
     * 获取csrf码
     */
    getCsrf(){
      getCsrf({client_id: this.clientId}).then((res) => {
        this.csrfCode = res.data.csrf;
      });
    },
  }
}
